# -*- coding: utf-8 -*-
"""
Web framework/Tornado RequestHandler Class/test04.py
"""

import tornado.web
import asyncio


class IndexHandler(tornado.web.RequestHandler):

    def prepare(self):
        self.r_method = self.request.method

    def get(self):
        """
        RequestHandler.get_argument
        :return:
        """
        params = self.get_argument(name="id", default="1")
        # > > default 不指定的场合
        # > curl http://127.0.0.1:8888/
        # > Http Response: <html><title>400: Bad Request</title><body>400: Bad Request</body></html>
        # > Console: 400 GET / (127.0.0.1): Missing argument id
        # > > default 指定的场合
        # > curl http://127.0.0.1:8888/
        # > Http Response: This is GET method. Params is 1
        r_message = f"This is {self.r_method} method. Params is {params}"
        print(r_message)
        return self.write(r_message)
        # > curl http://127.0.0.1:8888/?id=10
        # > Http Response: This is GET method. Params is 10

    def post(self):
        """
        RequestHandler.get_body_argument
        :return:
        """
        p_body = self.get_body_argument(name="id", default="3")
        r_message = f"This is {self.r_method} method. Body is {p_body}"
        print(r_message)
        return self.write(r_message)
        # > > default 不指定的场合
        # > curl -X POST http://127.0.0.1:8888/
        # > Http Response: <html><title>400: Bad Request</title><body>400: Bad Request</body></html>
        # > Console: 400 POST / (127.0.0.1): Missing argument id
        # > > default 指定的场合
        # > curl -X POST http://127.0.0.1:8888/
        # > Http Response: This is POST method. Body is 3


class TestHandler(tornado.web.RequestHandler):
    def prepare(self):
        self.r_method = self.request.method

    def get(self):
        """
        RequestHandler.get_arguments
        :return:
        """
        params = self.get_arguments(name="id")
        r_message = f"This is {self.r_method} method. Params is {params}"
        print(r_message)
        return self.write(r_message)
        # > curl http://127.0.0.1:8888/test/
        # > Http Response: This is GET method. Params is []
        # > curl http://127.0.0.1:8888/test/?id=10
        # > Http Response: This is GET method. Params is ['10']

    def post(self):
        """
        RequestHandler.get_body_arguments
        :return:
        """
        p_body = self.get_body_arguments(name="id")
        r_message = f"This is {self.r_method} method. Body is {p_body}"
        print(r_message)
        return self.write(r_message)
        # > curl -X POST http://127.0.0.1:8888/test/
        # > Http Response: This is POST method. Body is []
        # > curl -X POST -d "id=10" http://127.0.0.1:8888/test/
        # > Http Response: This is POST method. Body is ['10']


class QueryHandler(tornado.web.RequestHandler):
    def prepare(self):
        self.r_method = self.request.method

    def get(self):
        """
        RequestHandler.get_query_argument
        :return:
        """
        params = self.get_query_argument(name="id", default="1")
        r_message = f"This is {self.r_method} method. Params is {params}"
        print(r_message)
        return self.write(r_message)
        # > > default 不指定的场合
        # > curl http://127.0.0.1:8888/query/
        # > Http Response: <html><title>400: Bad Request</title><body>400: Bad Request</body></html>
        # > Console: 400 GET / (127.0.0.1): Missing argument id
        # > > default 指定的场合
        # > curl http://127.0.0.1:8888/query/
        # > Http Response: This is GET method. Params is 1


class IndexQueryHandler(tornado.web.RequestHandler):
    def prepare(self):
        self.r_method = self.request.method

    def get(self):
        """
        RequestHandler.get_query_arguments
        :return:
        """
        params = self.get_query_arguments(name="id")
        r_message = f"This is {self.r_method} method. Params is {params}"
        print(r_message)
        return self.write(r_message)
        # > curl http://127.0.0.1:8888/index/
        # > Http Response: This is GET method. Params is []
        # > curl http://127.0.0.1:8888/index/?id=10
        # > Http Response: This is GET method. Params is ['10']

    def post(self):
        """
        RequestHandler.request
        :return:
        """
        header = self.request.headers
        body = self.request.body
        method = self.request.body
        r_message = f"This is {method} method. Header is {header}, Body is {body}"
        print(r_message)
        return self.write(r_message)
        # > curl -X POST -d "id=10" http://127.0.0.1:8888/index/
        # > Http Response:
        # > This is b'id=10' method. Header is Host: 127.0.0.1:8888
        # > User-Agent: curl/7.79.1
        # > Accept: */*
        # > Content-Length: 5
        # > Content-Type: application/x-www-form-urlencoded
        # > , Body is b'id=10'


async def main():
    app = tornado.web.Application([
        (r'/', IndexHandler),
        (r'/test/', TestHandler),
        (r'/query/', QueryHandler),
        (r'/index/', IndexQueryHandler),
    ])
    app.listen(8888)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())
